from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, desc, SmallInteger,orm
from werkzeug.security import generate_password_hash, check_password_hash

from app.libs.error_code import NotFount, AuthFailed
from app.models.base import Base, db


class Case(Base):
    id = Column(Integer, primary_key=True)
    module_name=Column(String(50),unique=False, nullable=True) #功能模块名称，可为空
    title= Column(String(50),unique=False, nullable=False) #标题，不可为空
    priority=Column(Integer,nullable=False) #优先级 不可为空
    precondition=Column(String(50),unique=False, nullable=True) #前置条件，可为空
    operating_steps=Column(String(1000),unique=False, nullable=False)#操作步骤，不可为空
    expected_result=Column(String(50),unique=False, nullable=False) #预期结果，不可为空
    actual_result= Column(String(50),unique=False, nullable=False) #实际结果，不可为空
    test_result=Column(Integer) #测试结果 0是测试失败 1是测试成功 2是未开始测试
    bug=Column(String(1000),unique=False, nullable=True)

    @orm.reconstructor
    def __init__(self):
        self.fields = ['id', 'module_name', 'title','priority','precondition','operating_steps','expected_result','actual_result','bug','create_time','test_result']

    @staticmethod
    def add_case(module_name, title, priority,precondition,operating_steps,expected_result,actual_result,test_result,bug):
            if not priority:
                priority=4
            if not test_result:
                test_result=2
            with db.auto_commit():
                case = Case()
                case.module_name = module_name
                case.title = title
                case.priority = priority
                case.precondition = precondition
                case.operating_steps = operating_steps
                case.expected_result = expected_result
                case.actual_result = actual_result
                case.test_result = test_result
                case.bug = bug
                db.session.add(case)

    @staticmethod
    def update_case(case,module_name, title, priority,precondition,operating_steps,expected_result,actual_result,test_result,bug):
        case.module_name = module_name
        case.title = title
        case.priority = priority
        case.precondition = precondition
        case.operating_steps = operating_steps
        case.expected_result = expected_result
        case.actual_result = actual_result
        case.test_result = test_result
        case.bug = bug
        db.session.commit()

    @staticmethod
    def del_case(case):
        case.del_status=0
        db.session.commit()

    @staticmethod
    def select_case(id):
        case = Case.query.get(id)
        return case

